<?php

	$filename = "yahoorankings.data";
	$file = str_replace("\t", "|", file_get_contents("raw/" . $filename));

	$lineNum = 1;
	$lines = explode("\n", $file);
	$playerData = "";
	$playerList = "NAME|POS|RANK|FGM|FGA|FG%|FTM|FTA|FT%|3PTM|PTS|REB|AST|ST|BLK|TO|pFGM|pFGA|pFG%|pFTM|pFTA|pFT%|p3PTM|pPTS|pREB|pAST|pST|pBLK|pTO";
	foreach($lines as $line)
	{
		if ($lineNum == 8)
		{
			$lineNum = 0;
			$playerData = trim($playerData);
			$playerList .= "\n" . $playerData;
		}
		else if ($lineNum == 1)
		{
			$line = substr($line, 0, strpos($line, "–") - 1);
			$line = substr_replace($line, "|", strrpos($line, " ", -1), 1);
			$playerData = trim($line) . "|";
		}
		else if ($lineNum == 3 || $lineNum == 6 || $lineNum == 7)
		{
			$line = trim(substr($line, strpos($line, "|") + 1)) . "|";

			if ($lineNum == 3)
				$playerData = $line . $playerData;
			else
			{
				$line = str_replace("-", "--", $line);
				$playerData .= $line;
			}
		}
		$lineNum++;
	}

	if (file_put_contents("out/" . $filename, $playerList) !== false)
	{
		echo "<h3>Successfully processed raw/$filename.</h3>" .
			"<h3>Processed data is at out/$filename.</h3>";
		echo "<iframe src='out/$filename' width='100%' height='80%'>";
	}
	else
	{
		echo "<h3>Error processing raw data.</h3>";
	}
?>